/*#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Test(void)
{
    char *str = (char *)malloc(100);
    strcpy(str, "hello");
    free(str);
    if (str != NULL)
    {
        strcpy(str, "world");
        printf(str);
    }
}
int main()
{
    Test();
    return 0;
}*/
#define _CRT_SECURE_NO_WARNINGS 1
//#include "contact.h"
void LoadContact(Contact* pc)
{
	FILE*pf = fopen("contact.txt", "rb");
	if (pf == NULL)
	{
		perror("Load");
	}
	else
	{
		PeoInfo tmp = { 0 };
		int i = 0;
		while (fread(&tmp, sizeof(PeoInfo), 1, pf))
		{
			//增容
			check_capacity(pc);
			pc->data[i] = tmp;
			pc->sz++;
			i++;
		}
		fclose(pf);
		pf = NULL;
	}
}

void InitContact(Contact* pc)
{
	assert(pc);
	pc->sz = 0;
	PeoInfo *ptr= (PeoInfo*)calloc(DEFAULT_SZ, sizeof(PeoInfo));
	if (ptr == NULL)
	{
		perror("InitContact::calloc");
		return;
	}
	pc->data = ptr;
	pc->capacity = DEFAULT_SZ;
	//memset(pc->data, 0, sizeof(pc->data));
	//LoadContact();
}
//void AddContact(Contact* pc)
//{
//	assert(pc);
//	if (pc->sz == 100)
//	{
//		printf("FULL");
//		return;
//	}
//	printf("please input name:\n");
//	scanf("%s", pc->data[pc->sz].name);
//	printf("please input age:\n");
//	scanf("%d", &pc->data[pc->sz].age);
//	printf("please input sex:\n");
//	scanf("%s", pc->data[pc->sz].sex);
//	printf("please input address:\n");
//	scanf("%s", pc->data[pc->sz].add);
//	printf("please input phone-number:\n");
//	scanf("%s", pc->data[pc->sz].tele);
//	pc->sz++;
//
//}
void check_capacity(Contact* pc)
{
	if (pc->sz == pc->capacity)
	{
		PeoInfo*ptr=(PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ) * sizeof(PeoInfo));
		if (ptr == NULL)
		{
			perror("check_capacity::realloc");
			return;
		}
		pc->data = ptr;
		pc->capacity += INC_SZ;
	}
}
void AddContact(Contact* pc)
{
	assert(pc);
	check_capacity(pc);
	printf("please input name:\n");
	scanf("%s", pc->data[pc->sz].name);
	printf("please input age:\n");
	scanf("%d", &pc->data[pc->sz].age);
	printf("please input sex:\n");
	scanf("%s", pc->data[pc->sz].sex);
	printf("please input address:\n");
	scanf("%s", pc->data[pc->sz].add);
	printf("please input phone-number:\n");
	scanf("%s", pc->data[pc->sz].tele);
	pc->sz++;

}

void ShowContact(const Contact* pc)
{
	assert(pc);
	int i = 0;
	printf("%-8s\t%-8s\t%-8s\t%-8s\t%-8s\n", "name", "age", "sex", "address", "phone-number");
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-8s\t%-8d\t%-8s\t%-8s\t%-8s\n", 
			pc->data[i].name, 
			pc->data[i].age, 
			pc->data[i].sex, 
			pc->data[i].add, 
			pc->data[i].tele);
	}
}
int FindByName(Contact* pc, char name[])
{
	int i;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data->name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
void DelContact(Contact* pc)
{
	char name[10] = { 0 };
	if (pc->sz == 0)
	{
		printf("NULL\n");
		return;
	}
	printf("please input the name you want to delete:\n");
	scanf("%s", name);
	int i = 0;
	int del = 0;
	int ret = FindByName(pc, name);
	if (ret == -1)
	{
		printf("Not Found\n");
		return;
	}
	
	for (i = ret; i < pc->sz-1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	printf("Finshed\n");
	pc->sz--;
}
void SearchContact(Contact* pc)
{
	char name[10];
	printf("please input name you want to search:\n");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("Not Found\n");
		return;
	}
	printf("%-8s\t%-8s\t%-8s\t%-8s\t%-8s\n", "name", "age", "sex", "address", "phone-number");
	printf("%-8s\t%-8d\t%-8s\t%-8s\t%-8s\n", 
		pc->data[pos].name, 
		pc->data[pos].age, 
		pc->data[pos].sex, 
		pc->data[pos].add, 
		pc->data[pos].tele);
}
int CmpByName(const void* e1, const void* e2)
{
	return strcmp(((PeoInfo*)e1)->name,((PeoInfo*)e2)->name);
}
void SortContact(Contact* pc)
{
	qsort(pc->data, pc->sz, sizeof(PeoInfo), CmpByName);
	printf("Finshed\n");
}
void DestoryContact(Contact* pc)
{
	free(pc->data);
	pc->data = NULL;
	pc->capacity = 0;
	pc->sz = 0;
	pc = NULL;
}
void SaveContact(Contact* pc)
{
	FILE* pf = fopen("contact.txt", "wb");
	if (pf == NULL)
	{
		perror("Save");
	}
	else
	{
		int i;
		for (i = 0; i < pc->sz; i++)
		{
			fwrite(pc->data + i, sizeof(PeoInfo), 1, pf);
		}
		fclose(pf);
		pf = NULL;
		printf("FINSHED");
	}
}